package com.twu.biblioteca;

import java.io.PrintStream;

/**
 * Created by lbutcher on 31/07/2014.
 */
public enum BibliotecaOptions {
    INVALID("Invalid") {
      public void act(String input, PrintStream out) {
              out.println("Select a valid option!");
      }
    },

    LIST_BOOKS("List Books") {
        public void act(String input, PrintStream out) {
            String output = library.listBooks();
            out.println(output);
        }
    },

    LIST_MOVIES("List Movies") {
        public void act(String input, PrintStream out) {
            String output = library.listMovies();
            out.println(output);
        }
    },

    CHECKOUT("Checkout") {
        public void act(String input, PrintStream out) {
            if(libraryUser.getLoggedInUser() != null) {
                if(input.contains(" ")) {
                    String product = input.substring(input.indexOf(" ")).trim();
                    String output = library.checkOutProduct(product);
                    out.println(output);
                }
            }
            else {
                String output = libraryUser.printLogInMessage();
                out.println(output);
            }
        }
    },

    RETURN("Return") {
        public void act(String input, PrintStream out) {
            if(libraryUser.getLoggedInUser() != null) {
                if(input.contains(" ")) {
                    String product = input.substring(input.indexOf(" ")).trim();
                    out.println(library.returnProduct(product));
                }
            }
        }
    },

    LOGIN("Login") {
        public void act(String input, PrintStream out) {
            // Check for valid input
            if(input.contains(" "))
            {
                String loginDetails = input.substring(input.indexOf(" ")).trim();
                out.println(libraryUser.logInUser(loginDetails));
            }
        }
    },

    VIEW("View") {
        public void act(String input, PrintStream out) {
            if(libraryUser.getLoggedInUser() != null) {
                out.println(libraryUser.viewUserInformation(libraryUser.getLoggedInUser()));
            }
        }
    },

    QUIT("Quit") {
        public void act(String input, PrintStream out) {
            System.exit(0);
        }
    };

    public abstract void act(String input, PrintStream out);
    public static Library library = new Library();
    public static LibraryUser libraryUser = new LibraryUser();
    private final String option;

    private BibliotecaOptions(final String option) {
        this.option = option;
    }

    @Override
    public String toString() {
        return option;
    }

    public static int getOptionNumber(String option) {
        String[] splitInput = option.split(" ");
        String phraseToCheck = splitInput[0];

        for(int j = 1; j <= splitInput.length; j++) {
            for (int i = 0; i < values().length; i++) {
                if (phraseToCheck.equals(values()[i].option)) {
                    return i;
                }
            }
            phraseToCheck += " " + splitInput[j];
        }

        return 0;
    }
}
